#!/usr/bin/python
#author Eugeni Dodonov
 
import sys
import urllib2
import sys
import time
 
URL_BUG_XML="/show_bug.cgi?ctype=xml&excludefield=attachment&" # + list of bug_ids
URL_BUG_LIST="/buglist.cgi?query_format=advanced&chfieldto=Now&ctype=csv&chfieldfrom=" # + yyyy-mm-dd

bugtrackers = {
        "mandriva": {
            "url": "https://qa.mandriva.com",
            "start": 0,
            "end": 46200
            },
        "kde": {
            "url": "http://bugs.kde.org",
            "start": 0,
            "end": 177000
            },
        "kernel": {
            "url": "http://bugzilla.kernel.org",
            "start": 0,
            "end": 12200
            },
        }

def list_updated_bugs(url, lastdate):
    """Retrieves the list of bugs updated after a date"""
    list_url = url + URL_BUG_LIST + lastdate
    bugs = []
    # first line is the description of CSV format, just ignore it
    for line in urllib2.urlopen(list_url).readlines()[1:]:
        bug_id, descr = line.split(",", 1)
        bugs.append(bug_id)
    bugs.sort()
    return bugs

def get_bugs(name, url, output_dir, bugs, bugs_at_once=50, timestamp=None):
    """Retrieves given list of bugs from correspondent tracker"""
    if timestamp:
        # we need this to prevent overwriting overlaped bugs
        counter = 0
    for start in range(0, len(bugs), bugs_at_once):
        end = start + bugs_at_once
        if end >= len(bugs):
            end = len(bugs) - 1
        print "Getting %d bugs (%d/%d) [%s]: from %s to %s" % (bugs_at_once, start, len(bugs), name, bugs[start], bugs[end])
        curbugs = "id=" + "&id=".join(bugs[start:end])
        ddl_url = url + URL_BUG_XML + curbugs
        if not timestamp:
            fd = open("%s.%d.%d.xml" % (name, start, end), "w")
        else:
            fd = open("%s/%s.%s.%d.xml" % (output_dir, name, timestamp, counter), "w")
            counter += 1
        #data = urllib2.urlopen("http://bugs.kde.org/show_bug.cgi?ctype=xml&%s" % bugs).read() # kde bugs
        data = urllib2.urlopen(ddl_url).read() # mandriva bugs
        fd.write(data)
        fd.close()

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print "Usage %s <trackers separated by ,>  <last update date (yyyy-mm-dd> <output_dir_path>""" % sys.argv[0]
        sys.exit(1)

    trackers = sys.argv[1].split(",")
    output_dir = sys.argv[3]
    #if len(sys.argv) == 4:
    lastdate = sys.argv[2]
    try:
        # check if it is a valid date
        year, month, day = lastdate.split("-")
    except:
        print "Error: invalid last update date!"
        sys.exit(1)
    #else:
    #lastdate = None

    for name in trackers:
        try:
            tracker = bugtrackers[name]
        except:
            print "Invalid tracker: %s" % name
            continue
        url = tracker["url"]
        start = tracker["start"]
        end = tracker["end"]
        if not lastdate:
            timestamp = None
            bugs = [str(x) for x in range(start, end)]
        else:
            timestamp = str(int(time.time()))
            bugs = list_updated_bugs(url, lastdate)
        get_bugs(name, url, output_dir, bugs, timestamp=timestamp)
